package com.znyj.servlet.utilsTool;

import com.alibaba.fastjson.JSONObject;
import com.znyj.servlet.aes.AesException;
import com.znyj.servlet.aes.MsgCrypt;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.Random;

/**
 * Created by gaoliwei02 on 2017/11/3.
 */
public class HttpClientUtil {
    private static final Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);
    public static String url = "http://greatcong.com";

    public static String openDeviceApi(JSONObject jsonObject) throws InterruptedException, AesException, IOException, AesException {

        MsgCrypt msgCrypt = new MsgCrypt("tokenNoUse", "5lanyIYz49uJ3iqaKp8Zvd2j9xJ7rxZKRHBjcQeXBoq");
        String body = msgCrypt.encryptMsg(jsonObject.toString());

        HttpClient openClient = new DefaultHttpClient();
        HttpPost openPost = new HttpPost(url + "/crypt/openDevice");

        StringEntity s1 = new StringEntity(body);
        s1.setContentEncoding("UTF-8");
        s1.setContentType("multipart/form-data");
        openPost.setEntity(s1);

        HttpResponse response = openClient.execute(openPost);
        String openResult = EntityUtils.toString(response.getEntity());

        return msgCrypt.decryptMsg(openResult);
    }

    public static String getOrderInfoApi(long orderId) throws InterruptedException, AesException, IOException {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("orderId", orderId);

        MsgCrypt msgCrypt = new MsgCrypt("tokenNoUse", "5lanyIYz49uJ3iqaKp8Zvd2j9xJ7rxZKRHBjcQeXBoq");
        String body = msgCrypt.encryptMsg(jsonObject.toString());

        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(url + "/crypt/getOrderInfo");
        String result = null;
        try {
            StringEntity s = new StringEntity(body);
            s.setContentEncoding("UTF-8");
            s.setContentType("multipart/form-data");
            post.setEntity(s);

            HttpResponse response = client.execute(post);
            result = EntityUtils.toString(response.getEntity());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return msgCrypt.decryptMsg(result);
    }

    public static String getDeviceInfoApi(JSONObject jsonObject) throws InterruptedException, AesException {

        MsgCrypt msgCrypt = new MsgCrypt("tokenNoUse", "5lanyIYz49uJ3iqaKp8Zvd2j9xJ7rxZKRHBjcQeXBoq");
        String body = msgCrypt.encryptMsg(jsonObject.toString());

        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(url + "/crypt/getDeviceInfo");
        String result = null;
        try {
            StringEntity s = new StringEntity(body);
            s.setContentEncoding("UTF-8");
            s.setContentType("multipart/form-data");
            post.setEntity(s);

            HttpResponse response = client.execute(post);
            result = EntityUtils.toString(response.getEntity());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return msgCrypt.decryptMsg(result);
    }

    public static String getRandomString(int length) { //length表示生成字符串的长度
        String base = "abcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }
}
